<?php 
    $i_userId = osc_logged_user_id();
	if(Params::getParam('delete') != '' && osc_is_web_user_logged_in()){
		delete_item(Params::getParam('delete'), $i_userId);
	}

    $itemsPerPage = (Params::getParam('itemsPerPage') != '') ? Params::getParam('itemsPerPage') : 300;
    $iPage        = (Params::getParam('iPage') != '') ? Params::getParam('iPage') : 0;

    Search::newInstance()->addConditions(sprintf("%st_item_watchlist.fk_i_user_id = %d", DB_TABLE_PREFIX, $i_userId));
    Search::newInstance()->addConditions(sprintf("%st_item_watchlist.fk_i_item_id = %st_item.pk_i_id", DB_TABLE_PREFIX, DB_TABLE_PREFIX));
    Search::newInstance()->addTable(sprintf("%st_item_watchlist", DB_TABLE_PREFIX));
    Search::newInstance()->page($iPage, $itemsPerPage);

    $aItems      = Search::newInstance()->doSearch();
    $iTotalItems = Search::newInstance()->count();
    $iNumPages   = ceil($iTotalItems / $itemsPerPage) ;

    View::newInstance()->_exportVariableToView('items', $aItems);
    View::newInstance()->_exportVariableToView('search_total_pages', $iNumPages);
    View::newInstance()->_exportVariableToView('search_page', $iPage) ;

	// delete item from watchlist
	function delete_item($item, $uid){
		$conn = getConnection();
		$conn->osc_dbExec("DELETE FROM %st_item_watchlist WHERE fk_i_item_id = %d AND fk_i_user_id = %d LIMIT 1", DB_TABLE_PREFIX , $item, $uid);
	}
?>

 <div class="container user">
            <div class="row">
                <div class="span16 columns">
                    <?php twitter_show_flash_message() ; ?>
                    <?php twitter_user_menu() ; ?>
                </div>
            </div>
			<div class="tag_content">
			<h2><?php _e('Your favorite items', 'watchlist'); ?> <?php if(osc_count_items() > 0) { ?>
			 <span style="float:right;"><?php 			 
			 echo $iTotalItems ?>&nbsp;<?php _e('vehicles', 'twitter_bootstrap') ; ?></span><?php } ?>			
			</h2>
			<hr/>
    <div id="main">
        <?php if (osc_count_items() == 0) { ?>
        <div class="row">
                <div class="span16 columns" style="text-align:center;">
                    <h3><?php _e("You don't have any favorite vehicles yet", "watchlist"); ?></h3>
                    <br/>
                </div>
        </div>
        <?php } else { ?>
       
        <div class="ad_list">
           
                    <?php $class = "even"; ?>
                    <?php while ( osc_has_items() ) { ?>
					<div class="row">
                    <div class="span15 columns">
					<div class="photo">
						 <a href="<?php echo osc_item_url() ; ?>">
						<?php if( osc_count_item_resources() ) { ?>
					   
							<img src="<?php echo osc_resource_thumbnail_url() ; ?>" width="148px" height="110px" title="<?php echo osc_item_title(); ?>" alt="<?php echo osc_item_title(); ?>" />
					   
						<?php } else { ?>						
						sin fotos
						<?php } ?>
						 </a>
					</div>
					<div style="margin-left:20px;float:left;width:60%;">
                        <h4><a href="<?php echo osc_item_url(); ?>"><?php echo osc_item_title(); ?></a>
								<span class="label grey" style="text-transform: lowercase;margin-left:10px;"><?php _e("Ref", 'twitter_bootstrap'); ?> <?php echo osc_item_id(); ?></span>
						</h4>
						
                        <p class="gray"><?php printf(__('<strong>Publish date</strong>: %s', 'twitter_bootstrap'), osc_format_date( osc_item_pub_date() ) ) ; ?></p>
                        <?php
                            $location = array() ;
                            if( osc_item_country() != '' && count(osc_get_countries())>1) {
                                $location[] = sprintf( __('<strong>Country</strong>: %s', 'twitter_bootstrap'), osc_item_country() ) ;
                            }
                            if( osc_item_region() != '' ) {
                                $location[] = sprintf( __('<strong>Region</strong>: %s', 'twitter_bootstrap'), osc_item_region() ) ;
                            }
                            if( osc_item_city() != '' ) {
                                $location[] = sprintf( __('<strong>City</strong>: %s', 'twitter_bootstrap'), osc_item_city() ) ;
                            }
                            if( count($location) > 0) {
                        ?>
                        <p class="gray"><?php echo implode(' &middot; ', $location) ; ?></p>
                        <?php } ?>
                        <p><?php echo osc_highlight( strip_tags( osc_item_description() ) ) ; ?></p>
                      
						</div>
						<div style="float:right;">						
							<span class="label grey2" >
								<?php echo osc_item_category(); ?>
							</span>	
						</div>
						<br/><br/>
						<div style="float:right;">		
							<strong>
							 <a class="delete" onclick="javascript:return confirm('<?php _e('This action can not be undone. Are you sure you want to continue?', 'watchlist'); ?>')" href="<?php echo osc_render_file_url(osc_plugin_folder(__FILE__) . 'watchlist.php') . '?delete=' . osc_item_id(); ?>" >
                                                             <i class="icon-remove-circle" title="Remove vehicle from favorites"></i>&nbsp;<?php _e('Remove', 'watchlist'); ?></a>
							 </strong>
						</div>
					</div>
					</div>
					<hr/>
                    <?php $class = ($class == 'even') ? 'odd' : 'even'; ?>
                    <?php } ?>
                </tbody>
            </table>
        </div>
        <div class="paginate">
            <?php //echo osc_pagination(array('url' => osc_render_file_url(osc_plugin_folder(__FILE__) . 'watchlist.php') . '?iPage={PAGE}')); ?>
		
        </div>
        <?php } ?>
    </div>
	</div>
</div>